package edu.byu.intex2.data.dao;

import edu.byu.intex2.data.bo.EmployeeDAO; //What's this?
import edu.byu.intex2.data.Cache;
import edu.byu.intex2.data.ConnectionPool;
import edu.byu.intex2.data.DataException;
import java.sql.*;

/**
 * Employee Data Access Object
 * @author Adam Chavez
 */
public class EmployeeDAO {

  /////////////////////////////////////////////
  ///   Singleton code

  private static EmployeeDAO instance = null;

  /** Creates a new instance of EmployeeDAO */
  private EmployeeDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized EmployeeDAO getInstance() {
    if (instance == null) {
      instance = new EmployeeDAO();
    }
    return instance;
  }//getInstance

  ////////////////////////////////////////////
  ///   CREATE methods

  /**
   * Creates a new Employee in the database
   * set alreadyInDB to false
   * put Employee in cache
   */
  public EmployeeDAO create(String id) throws DataException {

  }//create


  ////////////////////////////////////////////
  ///   READ methods

  /**
   * Reads an existing Employee from the database
   * 1) Checks cache
   * 2) Gets a connection
   * 3) Calls read with connection argument
   */
  public EmployeeDAO read(String id) throws DataException {
  }

  /**
   * Internal method to read an existing Employee from the database
   * 1) Check cache for object
   * 2) Create a prepared statement to execute sql query
   * 3) Create a resultset from the preparedstatement
   * 4) Return the Employee
   */
  synchronized EmployeeDAO read(String id, Connection conn) throws Exception {

  }//read


  /**
   * Internal method to create a Employee object from a record
   * 1) Check cache for object
   * 2) Instantiate new Employee
   * 3) Set alreadyInDb to false
   * 4) Set all variables from result set
   * 5) Set dirty to true
   * 6) Return Employee
   */
  synchronized EmployeeDAO readRecord(ResultSet rs, Connection conn) throws Exception {
  }//readRecord

  /////////////////////////////////////////////
  ///   UPDATE methods
  
  /** Saves an existing employee in the database */
  public void save(Employee employee) throws DataException {
    //get a connection from the pool, save all of the sales first using the other employee method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!
	
  }
  
  /** Internal method to update a employee in the database */
  void save(Employee employee, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the employee object to cache
	//decide whether to update or insert into the database
	
    }  
  
  /** Saves an existing employee to the database */
  private void update(Employee employee, Connection conn) throws Exception {
    
	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement
	
  }
  
  /** Inserts a new employee into the database */
  private void insert(Employee employee, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement
	
  }  
  
  
  
  
  
  /////////////////////////////////////////////////
  ///   DELETE methods
  
  /** We do not support deleting of business objects in this application */
  public void delete(Employee employee) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }



  ////////////////////////////////////////////////
  ///   SEARCH methods

  /** Retrieves all conceptualcars from the database */
  public List<Employee> getAll() throws DataException {
    //establish a connection, use a prepared statement, plug it into the search method return a list of empoyees
    }
  }

  /** Internal method to search by certain fields */
  private List<Employee> search(PreparedStatement stmt, Connection conn) throws Exception {
    List<Employee> conceptualcar = new LinkedList<Employee>();
    try {
      ResultSet rs = stmt.executeQuery();
      while (rs.next()) {
        conceptualcar.add(readRecord(rs, conn));
      }
    }finally{
      stmt.close();
    }
    return conceptualcar;
  }

  public List<Employee> getByName(String fName, String lName) throws Data Exception{
	//establish connection and prepared statement run it through the search method, return a list of employees matching the name(maybe more than one)
  }
  
  public Employee getByEmployeeNumber(String EmpID) throws Data Exception{
	//establish connection and prepared statement run it through the search method, return an Employee matching the EmpID
  }



}
